package com.ujoku.controller;

import com.labillusion.core.platform.web.rest.RESTController;
import com.ujoku.UrlHelper.UrlHelperBase;
import com.ujoku.UrlHelper.UrlHelperSelector;
import com.ujoku.request.UrlHelperForm;
import com.ujoku.view.domain.UrlHelperView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * Created by Andy.Lian on 2015/3/10.
 */
@Controller
public class UrlHelperController extends RESTController {


    @RequestMapping(value = "/urlHelper", method = RequestMethod.POST)
    @ResponseBody
    public UrlHelperView parse(@RequestBody UrlHelperForm form) throws Exception {

        /*//activity
        if(form.getUrl().startsWith("/activity"))
        {
            UrlHelperView view = new UrlHelperView();
            view.setAction("activity");
            String[] array = form.getUrl().split("/");
            if(array.length > 2)
            {
                view.setPairs(new HashMap<>());
                view.getPairs().put("id", array[2]);
            }
            return view;
        }

        //商品详情
        if(form.getUrl().startsWith("/p")){
            UrlHelperView view = new UrlHelperView();
            view.setAction("p");
            String[] array = form.getUrl().split("/");
            if(array.length > 2)
            {
                view.setPairs(new HashMap<>());
                view.getPairs().put("id", array[2]);
            }
            return view;
        }

        //search
        if(form.getUrl().startsWith("/search")){
            UrlHelperView view = new UrlHelperView();
            view.setAction("search");


            int index = form.getUrl().indexOf("?");
            if(index == -1)
                return view;

            view.setPairs(new HashMap<>());
            String query = form.getUrl().substring(index + 1);
            String[] pairs = query.split("&");
            for (String pair : pairs) {
                int idx = pair.indexOf("=");
                if(idx == -1)
                    continue;
                String key = pair.substring(0, idx);
                String value = pair.substring(idx + 1);
                if(key.equalsIgnoreCase("q"))
                    key = "freeText";

                view.getPairs().put(key, value);
                //query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
            }

            return view;
        }

        //外链
        if(form.getUrl().startsWith("http://")){
            UrlHelperView view = new UrlHelperView();
            view.setAction("http");

            view.setPairs(new HashMap<>());
            view.getPairs().put("url", form.getUrl());

            return view;
        }

        return null;*/

        UrlHelperBase base = UrlHelperSelector.UrlHelperBuilder(form);
        if(base != null){
            return base.getView(form);
        }else{
            return null;
        }
    }


}
